package mylist
{
	import com.greensock.TweenLite;
	import com.ViewSet;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.net.Socket;
	import flash.utils.ByteArray;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	/**
	 * ...
	 * @author db0@qq.com
	 */
	public class DragItem extends Sprite
	{
		public static var willSend:Boolean = false;
		public static var main:DragItem;
		
		public function DragItem()
		{
			if (main)
			{
				logs.adds("DragItem()");
				return;
			}
			main = this;
			
			main.visible = false;
			mouseChildren = true;
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private var bg:Bitmap;
		private var photoBmp:Bitmap;
		private static const ww:int = IconItem.ww * 1.2;
		private static const hh:int = IconItem.hh * 1.2;
		private var dragContainer:Sprite = new Sprite();
		private var fileItem:FileItem;
		
		private function init(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			bg = new Bitmap(new BitmapData(stage.stageWidth, stage.stageHeight, true, 0x00ffffff));
			addChild(bg);
		}
		
		public function show(item:FileItem):void
		{
			main.visible = true;
			fileItem = item;
			photoBmp = new Bitmap(item.bmp.bitmapData);
			
			dragContainer.addChild(photoBmp);
			ViewSet.fullCenter(photoBmp, -ww / 2, -hh / 2, ww, hh);
			stage.addChild(main);
			dragContainer.x = mouseX;
			dragContainer.y = mouseY;
			if (willSend)
			{
				main.visible = false;
				
				showThis(null);
			}
			else
				showThis(null);
		}
		
		private function showThis(e:Event):void
		{
			logs.adds(e, "showThis()");
			if (willSend && MultiUploadBtns.header)
			{
				main.visible = true;
				stage.addEventListener(MouseEvent.MOUSE_UP, send);
				stage.addChild(MultiUploadBtns.main);
				stage.addChild(dragContainer);
				MultiUploadBtns.main.showHear();
				dragContainer.startDrag();
				if (ViewList.main.getChildByName("c" + IconItem.selectIndex))
					ViewList.main.getChildByName("c" + IconItem.selectIndex).visible = false; //if send ,hide the orignal icon;
			}
		}
		
		private function send(e:MouseEvent):void
		{
			logs.adds(e.type, "send()");
			stage.removeEventListener(MouseEvent.MOUSE_UP, send);
			
			if(willSend)
			if (fileItem.state == FileItem.UPLOADED)
			{
				//fileItem.state = 2;
			}
			else
			{
				logs.adds("send () Error : no send complete",fileItem.state);
				if(fileItem.state != FileItem.UPLOADING)fileItem.state = FileItem.ADDED;
				//backToList(e);
				//return;
			}
			
			main.visible = false;
			dragContainer.stopDrag();
			if (willSend && MultiUploadBtns.header )
			{
				
				if (mouseY < MultiUploadBtns.header.x + MultiUploadBtns.header.height && fileItem.state == FileItem.UPLOADED)
				{
					MySocket.sendShow(fileItem.size, fileItem.time, 0, backToList);
					
					logs.adds("send pc to show");
					ViewList.main.removeItem(IconItem.selectIndex); //if remove by successed sended
				}
				else
				{
					logs.adds("del:back to the list");
					main.visible = true; //if send failed; back to the orignal place;
					var obj:DisplayObject = ViewList.main.getChildByName("c" + IconItem.selectIndex);
					if (obj == null)
					{
						logs.adds("send() Error , no " + "c" + IconItem.selectIndex);
						return;
					}
					TweenLite.killTweensOf(dragContainer);
					var toPos:Point = obj.localToGlobal(new Point(obj.width / 2, obj.height / 2));
					TweenLite.to(dragContainer, .1, {x: toPos.x, y: toPos.y, scaleX: 1 / 1.2, scaleY: 1 / 1.2, onComplete: backToList});
				}
			}
			else
			{
				logs.adds("will not send error ,back to list");
				backToList(e);
			}
		
		}
		
		private function removeBmp():void
		{
			while (dragContainer.numChildren > 0)
			{
				dragContainer.removeChildAt(0);
			}
			photoBmp = null;
		
		}
		
		/**
		 * 还原
		 */
		public function backToList(e:Event = null):void
		{
			//if (e)
				//logs.adds(e.type);
			logs.adds(e, "backToList()");
			
			if (stage)
			{
				stage.removeEventListener(MouseEvent.MOUSE_UP, send);
				if (stage.contains(dragContainer))
					stage.removeChild(dragContainer);
				dragContainer.scaleX = dragContainer.scaleY = 1;
			}
			
			if (ViewList.main.getChildByName("c" + IconItem.selectIndex))
				ViewList.main.getChildByName("c" + IconItem.selectIndex).visible = true;
			main.visible = false;
			removeBmp();
			
			if (MultiUploadBtns.header)
				MultiUploadBtns.main.hideHear();
		
		}
	}

}